const errorType = require("../constants/error-type");
const capitalService = require("../service/capital.service");
const checkValue = async (ctx, next) => {
  const { type, ddescribe, income, expend, cash } = ctx.request.body;

  if (!type || !ddescribe || !income || !expend || !cash) {
    return ctx.app.emit("error", errorType.CAPITAL_BASE_VALUE_NOT_NULL, ctx);
  }
  await next();
};
const getAllCapital = async (ctx, next) => {
  try {
    let result;
    if (ctx.request.params.id) {
      result = await capitalService.getCapital(ctx.request.params.id);
    } else {
      result = await capitalService.getAllCapital();
    }
    if (!result.length) {
      return ctx.app.emit("error", errorType.CAPITAL_DATA_IS_NULL, ctx);
    }
    ctx.resultCapital = result;
    await next();
  } catch (err) {
    console.log(err);
  }
};
module.exports = {
  checkValue,
  getAllCapital
};
